<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
			<span class="layui-breadcrumb">
            <a href="javascript:;">文档</a>
            <a>
              <cite>公共文档</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div style="padding-top: 15px">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <a href="javascript:;"><i class="layui-icon layui-icon-read"></i>&nbsp;&nbsp;公共文档</a>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-hide" id="wordTable" lay-filter="test"></table>
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="add">
                            <i class="layui-icon layui-icon-add-1"></i>添加</a>
                        <a class="layui-btn layui-btn-xs" lay-event="edit">
                            <i class="layui-icon layui-icon-edit"></i>编辑</a>
                        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">
                            <i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['laydate', 'form', 'laypage', 'layer', 'table', 'layedit'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
        var laypage = layui.laypage;
        var layer = layui.layer;
        var table = layui.table;

        var wordAdd,wordEdit;
        var wordId;

        //实例化一个table
        table.render({
            elem: '#wordTable'
            , height: 300
            ,id:'wordTable'
            ,url: 'http://localhost:8080/word/list' //数据接口
            , title: '文档列表'
            , page: true //开启分页
            // , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,defaultToolbar:['filter', 'exports']
            , cols: [[ //表头
                {
                    type: 'radio',
                    fixed: 'left'
                }, {
                    field: 'id',
                    title: 'ID',
                    width: '10%',
                    hide: true,
                }, {
                    field: 'wordTitle',
                    title: '文档标题',
                    width: '20%',
                    sort: true,
                    minWidth:100
                }, {
                    field: 'wordCreater',
                    title: '创建者',
                    width: '10%',
                    sort: true
                }, {
                    field: 'wordCreateDate',
                    title: '创建时间',
                    width: '10%',
                    sort: true
                }, {
                    field: 'wordEditer',
                    title: '编辑者',
                    width: '10%',
                    sort: true
                }, {
                    field: 'wordEditDate',
                    title: '编辑时间',
                    width: '10%',
                    sort: true
                }, {
                    field: 'wordType',
                    title: '类型',
                    width: '10%',
                    sort: true
                },{
                    field: 'wordKeyword',
                    title: '关键字',
                    hide: true
                },{
                    title:'操作',
                    fixed: 'right',
                    width: '25%',
                    minWidth: 100,
                    align: 'center',
                    toolbar: '#barDemo'
                }
            ]]
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'edit') {
                wordId=data.id;
                $("#wordLib_edit").val(data.wordLib);
                $("#wordTitle_edit").val(data.wordTitle);
                $("#wordKeyword_edit").val(data.wordKeyword);
                $("#wordContent_edit").val(data.wordContent);
                wordEdit=layer.open({
                    type:1,
                    title:'修改文档',
                    area:['600px','500px'],
                    content:$('#word_edit')
                });
            }
            else if(layEvent==='add'){
                wordAdd=layer.open({
                    type:1,
                    title:'添加文档',
                    area:['600px','500px'],
                    content:$('#word_add')
                });
            }
            else if(layEvent==='del'){
                layer.confirm('删除后将不可恢复',{icon:3,title:'提示'}, function(index){
                    obj.del();
                    layer.close(index);
                    $.ajax({
                        url: 'http://localhost:8080/word/delete',
                        data: data,
                        contentType: "application/x-www-form-urlencoded",
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function(data) {
                            if(data.status == "success") {
                                layer.msg('删除成功', {
                                    icon: 1,
                                    time: 2000
                                }, function() {
                                    //添加用户成功后关闭弹窗并刷新
                                    layer.close(index);
                                    table.reload('wordTable',{
                                        url:'http://localhost:8080/word/list'
                                    })
                                });
                            } else {
                                layer.msg(data.data.errMsg, {
                                    icon: 5,
                                    time:1000
                                });
                            }
                        }
                    });
                    return false;
                });
            }
        });

        form.on('submit(create_word)',function (obj) {
            //获取表单数据
            var field = obj.field;
            field.wordType=$('#wordType input[name="type"]:checked ').val();
            //获取当前时间并格式化yyyy-MM-dd HH:mm:ss
            var myDate=new Date();
            var nowDate=myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+
                myDate.getDate()+" "+myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
            field.wordCreateDate=nowDate;
            //获取单选框的值
            // field.type_edit=$('#teamStatus_edit input[name="type_edit"]:checked ').val()
            $.ajax({
                url: 'http://localhost:8080/word/create',
                data: field,
                contentType: "application/x-www-form-urlencoded",
                type: 'POST',
                xhrFields: {
                    withCredentials: true
                },
                success: function(data) {
                    if(data.status == "success") {
                        layer.msg('创建文档成功', {
                            icon: 1,
                            time: 2000
                        }, function() {
                            layer.close(wordAdd);
                            table.reload('wordTable',{
                                url:'http://localhost:8080/word/list'
                            })
                        });
                    } else {
                        layer.msg(data.data.errMsg, {
                            icon: 5,
                            time:2000
                        });
                    }
                }
            });
            return false;
        });

        form.on('submit(edit_word)',function (obj) {
            //获取表单数据
            var field = obj.field;
            field.type_edit=$('#wordType_edit input[name="type_edit"]:checked ').val();
            field.id=wordId;
            //获取当前时间并格式化yyyy-MM-dd HH:mm:ss
            var myDate=new Date();
            var nowDate=myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+
                myDate.getDate()+" "+myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
            field.wordEditDate_edit=nowDate;
            //获取单选框的值
            // field.type_edit=$('#teamStatus_edit input[name="type_edit"]:checked ').val()
            $.ajax({
                url: 'http://localhost:8080/word/update',
                data: field,
                contentType: "application/x-www-form-urlencoded",
                type: 'POST',
                xhrFields: {
                    withCredentials: true
                },
                success: function(data) {
                    if(data.status == "success") {
                        layer.msg('修改文档成功', {
                            icon: 1,
                            time: 2000
                        }, function() {
                            layer.close(wordEdit);
                            table.reload('wordTable',{
                                url:'http://localhost:8080/word/list'
                            })
                        });
                    } else {
                        layer.msg(data.data.errMsg, {
                            icon: 5,
                            time:2000
                        });
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
<!--添加文档-->
<div style="display: none;" id="word_add">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <h3>所属文档库</h3>
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" name="wordLib" id="wordLib" disabled autocomplete="off" class="layui-input" value="公共文档">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>文档标题
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="wordTitle" name="wordTitle" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>文档类型
                    </label>
                    <div class="layui-input-inline" id="wordType">
                        <input type="radio" name="type" value="文档" title="文档" lay-filter="type" checked>
                        <input type="radio" name="type" value="链接" title="链接" lay-filter="type">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="x-red">*</span>文档内容</label>
                    <div class="layui-input-block">
                        <textarea id="wordContent" name="wordContent" class="layui-textarea" placeholder="请输入内容" ></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>关键字
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="wordKeyword" name="wordKeyword" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                    </label>
                    <button  class="layui-btn layui-btn-radius layui-btn-fluid" lay-filter="create_word" lay-submit="">
                        创建
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<!--修改文档-->
<div style="display: none;" id="word_edit">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <h3>所属文档库</h3>
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" name="wordLib_edit" id="wordLib_edit" disabled autocomplete="off" class="layui-input" value="公共文档">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>文档标题
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="wordTitle_edit" name="wordTitle_edit" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>文档类型
                    </label>
                    <div class="layui-input-inline" id="wordType_edit">
                        <input type="radio" name="type_edit" value="文档" title="文档" lay-filter="type" checked>
                        <input type="radio" name="type_edit" value="链接" title="链接" lay-filter="type">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="x-red">*</span>文档内容</label>
                    <div class="layui-input-block">
                        <textarea id="wordContent_edit" name="wordContent_edit" class="layui-textarea" placeholder="请输入内容" ></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>文档关键字
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="wordKeyword_edit" name="wordKeyword_edit" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                    </label>
                    <button  class="layui-btn layui-btn-radius layui-btn-fluid" lay-filter="edit_word" lay-submit="">
                        修改
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
</html>